#! /bin/bash
#   
# memcached:    MemCached Daemon   
#   
# chkconfig:    - 90 25  
# description:  MemCached Daemon   
#   
### BEGIN INIT INFO
# Provides:          memcached
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Should-Start:        $local_fs
# Should-Stop:        $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description:    memcached - Memory caching daemon
# Description:        memcached - Memory caching daemon
### END INIT INFO

PORT=11211
USER=memcache
MAXCONN=1024
CACHESIZE=64
OPTIONS=""
memcached_location=/usr/local/memcached

RETVAL=0
prog="memcached"

#build pid
[ -f "/var/run/memcached.pid" ] || touch /var/run/memcached.pid
chown memcache.memcache /var/run/memcached.pid 

start () {
    echo -n $"Starting $prog: "
    $memcached_location/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS
    RETVAL=$?
    echo
	[ ! -d "/var/lock/subsys" ] && mkdir -p /var/lock/subsys/
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
    echo -n $"Stopping $prog: "
    local pid=`cat /var/run/memcached.pid`
    [ ".${pid}" != "." ] && kill ${pid}
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
        rm -f /var/lock/subsys/memcached
        rm -f /var/run/memcached.pid
    fi
}

restart () {
    stop
    start
}


# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
    stop
    ;;
    restart|reload)
    restart
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|reload}"
    exit 1
esac

exit $?
